/**
 * @namespace
 */
$.aws.ui = {
	/**
	 * Prepends UI's panel and subscribes to various events.
	 */
	init: function() {
		$('<div id="aws"><div class="container"><ul class="path" /></div></div>').prependTo($('body') );
		$.aws.observer.subscribe('keypress', 'keypress-ui', $.aws.ui.keypress);
		$.aws.observer.subscribe('rootWaypointSet', 'rootWaypointSet-ui', $.aws.ui.rootWaypointSet);
		$.aws.observer.subscribe('goneToNextWaypoint', 'goneToNextWaypoint-ui', $.aws.ui.goneToNextWaypoint);
		$.aws.observer.subscribe('goneToPreviousWaypoint', 'goneToPreviousWaypoint-ui', $.aws.ui.goneToPreviousWaypoint);
		$.aws.observer.subscribe('waypointValueChanged', 'waypointValueChanged-ui', $.aws.ui.waypointValueChanged);
		$.aws.observer.subscribe('rootWaypointUnset', 'rootWaypointUnset-ui', $.aws.ui.rootWaypointUnset);
		$.aws.observer.subscribe('reset', 'reset-ui', $.aws.ui.reset);
		$.aws.ui.initiated = true;
	},
	/**
	 * Simply shows UI panel
	 */
	show: function() {
		$('#aws').show();
		$.aws.ui.visible = true;
	},
	/**
	 * Hides UI panel
	 */
	hide: function() {
		$('#aws').hide();
		$.aws.ui.visible = false;
	},
	/**
	 * Hides UI panel and removes info about waypoints. Designed to respond to global reset event.
	 */
	reset: function() {
		$.aws.ui.hide();
		$('#aws ul.path').empty();
	},
	/**
	 * Prevents browser to go to previous page when it should not
	 * @param {String} code Code of key pressed
	 * @param {Object} e Event
	 */
	keypress: function(code, e) {
		if (code == 8 && ($.aws.waypoint || $.aws.ui.preventNextBackspace) ) {
			e.preventDefault();
			delete $.aws.ui.preventNextBackspace;
		}
	},
	/**
	 * Appends info about new waypoint to UI panel.
	 */
	goneToNextWaypoint: function() {
		var html = '<li>';
		if (typeof $.aws.waypoint.value != 'undefined') {
			 html = '<li class="value">';
		}
		html += '<span class="waypoint-name">' + $.aws.waypoint.name + '</span>';
		if ($.aws.waypoint.onEnter) {
			html += '<span class="waypoint-onenter">↳ ' + $.aws.waypoint.onEnter.description + '</span>';
		}
		if ($.aws.waypoint.next) {
			html += '<span class="waypoint-next">→ '
			if (typeof $.aws.waypoint.next.value != 'undefined') {
				html += 'enter value';
			} else {
				var allnext = [];
				$.each($.aws.waypoint.next, function(k, v) {
					allnext.push(v.name + '(' + k + ')');
				});
				html += allnext.join(', ');
			}
			html += '</span>';
		}
		html += '<span class="waypoint-description">' + $.aws.waypoint.description + '</span>';
		html += '</li>';
		$('#aws ul.path').append(html);
	},
	/**
	 * Removes info about last waypoint from UI panel
	 */
	goneToPreviousWaypoint: function() {
		$('#aws ul.path li:last').remove();
	},
	/**
	 * Displays new value of waypoint in UI panel
	 */
	waypointValueChanged: function() {
		$('#aws ul.path li:last span.waypoint-name').text($.aws.waypoint.value);
	},
	/**
	 * Shows UI panel
	 */
	rootWaypointSet: function() {
		$.aws.ui.show();
	},
	/**
	 * Resets UI panel and temporary flag to inform ui.keypress to ignore next backspace, because it will be executed after resetting.
	 */
	rootWaypointUnset: function() {
		$.aws.ui.reset();
		$.aws.ui.preventNextBackspace = true;
	}
};

/** Module should be initialized on global init event */
$.aws.observer.subscribe('init', 'init-ui', $.aws.ui.init);
